import lightgbm as lgb
from tiancheng.base.base_helper import *
from sklearn.model_selection import StratifiedKFold

y = get_tag_train_new()  # pd.read_csv('input/tag_train_new.txt')
sub = get_sub()
# from tiancheng.base.get_ftr_v02 import get_ftrs
# get_ftrs()
train = pd.read_csv(train_data_path)
label = get_tag_train_new()[tag_hd.Tag].values
test = pd.read_csv(test_data_path)
# train['Tag'] = y[tag_hd.Tag]
# tr_corr = train.corr()[tag_hd.Tag].reset_index()
# tr_corr
print(test.shape)
cols = ['12dayip1_y', '333daymerchant_x', '13dayip1_x', '8day_x', '526balmerchant_x', '371trans_amtmerchant_x', '31mode_y', '335daymerchant', '340dayip1_y', 'avg_op_day_time_op_0', '409code1merchant_x', '334daymerchant_y', '28daymac2_y', '599market_codegeo_code_x', '136mac1geo_code_x', '132mac1ip1_x', '128device_code3geo_code_x', '29daymac2', '33modeip1_y', 'time_op_1', 'mode_-1', '164wifi_x', '129device_code3geo_code_y', '572acc_id3geo_code_y', '370trans_amtmerchant_y', '133mac1ip1_y', '15dayip1_x', '510ip1merchant_y', '368trans_amtmerchant_y', 'time_sub_min', '166wifiip1_x', '324day_y', '6day_x', 'avg_op_day_time_op_2', '326day_y', '366trans_amt', '348daymac1_x', '462device_code2merchant_y', '364trans_amt_x', '21daymac1_y', 'amt_channel_min_140', '441acc_id1merchant_y', '122device_code3ip1_x', '4day_x', 'avg_time0_trans', 'time_op_0', '576geo_codemerchant_y', '330daymerchant_y', '191ip1_subgeo_code_y', '339dayip1_x', 'avg_trans_amt_ip1_count_tst', 'wifi_day', '123device_code3ip1_y', '546amt_src2merchant_y', '10dayip1_y', '20daymac1_x', '92device2ip1_x', '177geo_codeip1_y', '477device_code3geo_code_x', 'avg_trans_market_type_mean_0.0', '331daymerchant_x', '327day_x', '89device1geo_code_y', '452device_code1merchant_y', 'time_sub_max', '378trans_amtip1_x', '138mac2_x', '37modemac2_y', '482device1merchant_y', 'avg_trans_day_market_type_mean_0.0', '328day_y', '562acc_id2geo_code_y', '275success_group_rateip1_y', '137mac1geo_code_y', '176geo_codeip1_x', '472device_code3merchant_y', '392amt_src1ip1_x', '375trans_amtip1_y', '79versiongeo_code_y', '341dayip1_x', '53timeip1_y', '214day_bad_rateip1_x', '533balip1_x', '22daymac1_x', '342dayip1_y', '478device_code3geo_code_y', '113device_code2ip1_y', '141mac2ip1_y', 'avg_op_day_time_op_3', '547amt_src2ip1_x', 'time_sub_mean', '311os_group_rategeo_code_y', '73versionip1_y', '502mac1merchant_y', 'day_sub_mean', '27daymac2_x', '338dayip1_y', '506mac1geo_code_y', '25daymac2_x', 'amt_channel_mean_140', '172wifigeo_code_x', '524balmerchant_x', 'device_day', '259success_woemac2_y', '77versionmac2_y', '457device_code1geo_code_x', '19daymac1_y', '173wifigeo_code_y', '362trans_amt_x', '26daymac2_y', '39modegeo_code_y', '568acc_id3ip1_y', 'market_type_mean_0.0', 'amt_channel_max_140', '615ip1_submerchant_y', '438acc_id1_x', '401merchantip1_y', '83device1ip1_y', '325day_x', '471device_code3merchant_x', '387trans_amtmac1', '298os_bad_ratemac2_x', '153ip1geo_code_y', 'avg_time2_trans', '103device_code1ip1_y', '492device2merchant_y', '40success_x', '93device2ip1_y', '349daymac1_y', 'time_op_2', '281success_group_rategeo_code_y', 'avg_trans_day_merchant', '51time_y', '235day_minip1_y', '498device2geo_code_y', 'time_op_3', '110device_code2_x', '464device_code2ip1_y', '174geo_code_x', '67osmac2_y', '112device_code2ip1_x', '509ip1merchant_x', '119device_code2geo_code_y', '250day_maxgeo_code_x', '147ip1_y', '332daymerchant_y', '90device2_x', '594market_codemerchant_y', 'time_1', '355timeip1_y', '24daymac2_y', 'mode_1', '99device2geo_code_y', '120device_code3_x', '183ip1_sub_y', '504mac1ip1_y', '145mac2geo_code_y', '171wifimac2_y', '593market_codemerchant_x', 'day_sub_max', '465device_code2mac1_x', 'trans_type2_count', '117device_code2mac2_y', '109device_code1geo_code_y', '595market_codeip1_x', '170wifimac2_x', '458device_code1geo_code_y', '459device_code2_x', '140mac2ip1_x', '347daymac1_y', '337dayip1_x', '468device_code2geo_code_y', '305os_group_rateip1_y', '494device2ip1_y', '181geo_codemac2_y', '385trans_amtmac1_x', '345daymac1_y', '543amt_src2_x', '443acc_id1ip1_y', '343daymac1_y', '451device_code1merchant_x', '473device_code3ip1_x', '565acc_id3merchant_x', '361trans_amt_y', '80device1_x', '433trans_type1ip1_y', '100device_code1_x', '127device_code3mac2_y', '383trans_amtmac1_x', '3day_y', '665day_bad_ratemerchant_y', '467device_code2geo_code_x', '835market_type_woemerchant_y', 'market_type_min_2.0', '151ip1mac2_y', '859market_type_group_ratemac1_y', '699day_maxmac1_y', '98device2geo_code_x', '461device_code2merchant_x', 'avg_time3_trans', '573geo_code_x', 'avg_trans_amt_channel_mean_102', '649channel_group_ratemac1_y', '96device2mac2_x', '189ip1_submac2_y', '528balmerchant', '403merchantmac1_y', '144mac2geo_code_x', '395amt_src1mac1_y', 'mark_code_day', '382trans_amtmac1_y', '550amt_src2mac1_y', '102device_code1ip1_x', '580geo_codemac1_y', 'merchant_count', '442acc_id1ip1_x', '613ip1_sub_y', '815trans_type2_bad_ratemerchant_y', '107device_code1mac2_y', '118device_code2geo_code_x', '575geo_codemerchant_x', '351time_y', '508ip1_y', '57timemac2_y', 'market_type_mean_2.0', 'trans_amt_all', '549amt_src2mac1_x', '558acc_id2ip1_y', '399merchant_y', 'acc_id1_day', 'avg_trans_mat_bal', '474device_code3ip1_y', '357timemac1_y', '512ip1mac1_y', '87device1mac2_y', 'avg_trans_market_type_mean_2.0', '180geo_codemac2_x', '116device_code2mac2_x', '499mac1_x', '661day_woegeo_code_y', 'geo_code_day', '530balip1_y', 'avg_trans_day_merchant_count', '501mac1merchant_x', '466device_code2mac1_y', '344daymac1_x', '567acc_id3ip1_x', '221day_bad_rategeo_code_y', '505mac1geo_code_x', '759bal_woemac1_y', 'avg_trans_day_amt_channel_mean_102', '59timegeo_code_y', '108device_code1geo_code_x', '731trans_amt_group_rategeo_code_y', '579geo_codemac1_x', '588trans_type2mac1_y', '619ip1_submac1_y', 'avg_trans_day_market_type_mean_2.0', '469device_code3_x', '483device1ip1_x', '662day_bad_rate_x', 'channel_day_count', '188ip1_submac2_x', '456device_code1mac1_y', 'market_type_min_1.0', '540balmac1_x', '365trans_amt_y', '557acc_id2ip1_x', '97device2mac2_y', '72versionip1_x', 'avg_trans_market_type_mean_1.0', '481device1merchant_x', 'market_type_max_1.0', '564acc_id3_y', '185ip1_subip1_y', '537balmac1_y', 'market_type_max_0.0', 'market_type_max_2.0', '518bal_y', 'ip1_day_tst', '479device1_x', '435trans_type1mac1_y', '597market_codemac1_x', 'market_type_mean_1.0', '598market_codemac1_y', '542balmac1', '78versiongeo_code_x', '152ip1geo_code_x', '431trans_type1merchant_y', '445acc_id1mac1_y', 'avg_trans_day_market_type_mean_1.0', '106device_code1mac2_x', '559acc_id2mac1_x', '444acc_id1mac1_x', '82device1ip1_x', 'acc_id3_count', '496device2mac1_y', '126device_code3mac2_x']
train = train[cols].values
test = test[cols].values

print(train.shape)
print(test.shape)
skf = StratifiedKFold(n_splits=8, random_state=2018, shuffle=True)
best_score = []

oof_preds = np.zeros(train.shape[0])
sub_preds = np.zeros(test.shape[0])
res, weights = pd.DataFrame(), []
for index, (train_index, test_index) in enumerate(skf.split(train, label)):
    print(index)
    lgb_model = lgb.LGBMClassifier(boosting_type='gbdt', num_leaves=300, reg_alpha=3, reg_lambda=5, max_depth=-1,
                                   n_estimators=5000, objective='binary', subsample=0.95, colsample_bytree=0.818,
                                   subsample_freq=1, learning_rate=0.03,
                                   class_weight='balanced',
                                   random_state=1000 + index, n_jobs=4, min_child_weight=8, min_child_samples=8,
                                   min_split_gain=0.2)
    lgb_model.fit(train[train_index], label[train_index], verbose=10,
                  eval_metric="binary_logloss",
                  eval_set=[(train[train_index], label[train_index]),
                            (train[test_index], label[test_index])], early_stopping_rounds=30)
    oof_preds[test_index] = lgb_model.predict_proba(train[test_index], num_iteration=lgb_model.best_iteration_)[:, 1]
    m = tpr_weight_funtion(y_predict=oof_preds[test_index], y_true=label[test_index])
    print(m)
    # if m < 0.82:
    #     continue
    test_pred = lgb_model.predict_proba(test, num_iteration=lgb_model.best_iteration_)[:, 1]
    weights.append(m)
    res[index] = test_pred
    # sub_preds += test_pred / 5
    # print('test mean:', test_pred.mean())
    # predict_result['predicted_score'] = predict_result['predicted_score'] + test_pred

print(weights)
res.columns = [i for i in range(res.shape[1])]
blending_model(res, weights, "baseline12_")
print(123)
# index = weights.index(max(weights))
# sub['Tag'] = res[index]
# print(max(weights))
# sub.to_csv(sub_base_path + 'baseline10_%s.csv' % str(weights[index]), index=False)
